package com.example.projectmobile;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends FragmentActivity implements LocationListener{

	private double lat;
	private double lon;
	private String name;
	private GoogleMap gMap;
	private LocationManager locationManager;
	private String provider;
	private double nowlat;
	private double nowlon;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		Bundle bundle = getIntent().getExtras();//get lat lon name
		lat = bundle.getDouble("mapLat");
		lon = bundle.getDouble("mapLon");
		name = bundle.getString("mapName");
		
		gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_canvas)).getMap();
		gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		drawPathMap();
	}
	
	private void drawPathMap() {
		gMap.clear();
		
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);

		Location location = locationManager
				.getLastKnownLocation(provider);

		if (location != null) {

			System.out.println("Provider " + provider
					+ " has been selected.");

			onLocationChanged(location);

		} else {
			System.out.println("location == null");
			nowlat = 0;
			nowlon = 0;
		}
		
		MarkerOptions marker = new MarkerOptions();//marker select place
		marker.title(name);
		marker.position(new LatLng(lat, lon));
		gMap.addMarker(marker);
		
		MarkerOptions nowmarker = new MarkerOptions();//marker now point
		nowmarker.title("You are here");
		nowmarker.position(new LatLng(nowlat, nowlon));
		gMap.addMarker(nowmarker);
		
		gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 15.0f));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		nowlat = location.getLatitude();
		nowlon = location.getLongitude();
		
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

}
